package com.evan.review.architecture.mvi;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;
import com.evan.review.architecture.mvi.intent.CounterIntent;
import com.evan.review.architecture.mvi.viewmodel.CounterViewModel;
import com.evan.review.architecture.mvi.viewstate.CounterViewState;

import io.reactivex.rxjava3.disposables.CompositeDisposable;


/**
 * MVI架构示例Activity
 * Model-View-Intent模式
 */
public class MVIExampleActivity extends AppCompatActivity {

    private CounterViewModel viewModel;
    private final CompositeDisposable disposables = new CompositeDisposable();
    
    private TextView tvCounter;
    private Button btnIncrement;
    private Button btnDecrement;
    private Button btnReset;
    private ProgressBar progressBar;
    private TextView tvExplanation;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvi_example);
        
        // 初始化视图
        initViews();
        
        // 初始化ViewModel
        viewModel = new CounterViewModel();
        
        // 设置点击事件
        setupClickListeners();
        
        // 订阅状态更新
        subscribeToViewState();
    }
    
    private void initViews() {
        tvCounter = findViewById(R.id.tv_counter);
        btnIncrement = findViewById(R.id.btn_increment);
        btnDecrement = findViewById(R.id.btn_decrement);
        btnReset = findViewById(R.id.btn_reset);
        progressBar = findViewById(R.id.progress_bar);
        tvExplanation = findViewById(R.id.tv_explanation);
    }
    
    private void setupClickListeners() {
        btnIncrement.setOnClickListener(v -> 
                viewModel.processIntents(new CounterIntent.IncrementIntent()));
        
        btnDecrement.setOnClickListener(v -> 
                viewModel.processIntents(new CounterIntent.DecrementIntent()));
        
        btnReset.setOnClickListener(v -> 
                viewModel.processIntents(new CounterIntent.ResetIntent()));
    }
    
    private void subscribeToViewState() {
        disposables.add(viewModel.states().subscribe(this::render));
    }
    
    private void render(CounterViewState state) {
        // 根据状态更新UI
        tvCounter.setText(String.valueOf(state.getCount()));
        
        // 处理加载状态
        progressBar.setVisibility(state.isLoading() ? View.VISIBLE : View.GONE);
        
        // 处理错误状态
        if (state.getError() != null) {
            tvExplanation.setText("错误: " + state.getError());
            tvExplanation.setVisibility(View.VISIBLE);
        } else {
            tvExplanation.setText(getExplanationText(state));
            tvExplanation.setVisibility(View.VISIBLE);
        }
        
        // 启用/禁用按钮
        boolean buttonsEnabled = !state.isLoading();
        btnIncrement.setEnabled(buttonsEnabled);
        btnDecrement.setEnabled(buttonsEnabled);
        btnReset.setEnabled(buttonsEnabled);
    }
    
    private String getExplanationText(CounterViewState state) {
        if (state.getCount() > 0) {
            return "计数为正数";
        } else if (state.getCount() < 0) {
            return "计数为负数";
        } else {
            return "计数为零";
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposables.clear();
    }
} 